home *** CD-ROM | disk | FTP | other *** search
/ Languguage OS 2 / Languguage OS II Version 10-94 (Knowledge Media)(1994).ISO / gnu / gnuchess.lha / Xchess / jail.c < prev    next >
C/C++ Source or Header  |  1990-05-13  |  8KB  |  329 lines

  1.  
  2. /* This file contains code for X-CHESS.
  3.    Copyright (C) 1986-1990 Free Software Foundation, Inc.
  4.  
  5. This file is part of X-CHESS.
  6.  
  7. X-CHESS is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY.  No author or distributor
  9. accepts responsibility to anyone for the consequences of using it
  10. or for whether it serves any particular purpose or works at all,
  11. unless he says so in writing.  Refer to the X-CHESS General Public
  12. License for full details.
  13.  
  14. Everyone is granted permission to copy, modify and redistribute
  15. X-CHESS, but only under the conditions described in the
  16. X-CHESS General Public License.   A copy of this license is
  17. supposed to have been given to you along with X-CHESS so you
  18. can know your rights and responsibilities.  It should be in a
  19. file named COPYING.  Among other things, the copyright notice
  20. and this notice must be preserved on all copies.  */
  21.  
  22.  
  23. /* RCS Info: $Revision: 1.3 $ on $Date: 86/11/26 12:09:54 $
  24.  *           $Source: /users/faustus/xchess/RCS/jail.c,v $
  25.  * Copyright (c) 1986-1990 Wayne A. Christopher, U. C. Berkeley CAD Group
  26.  *    Permission is granted to do anything with this code except sell it
  27.  *    or remove this message.
  28.  *
  29.  */
  30.  
  31. #include "xchess.h"
  32.  
  33. #include "pawn_small.bitmap"
  34. #include "rook_small.bitmap"
  35. #include "knight_small.bitmap"
  36. #include "bishop_small.bitmap"
  37. #include "queen_small.bitmap"
  38. #include "king_small.bitmap"
  39.  
  40. #include "pawn_small_outline.bitmap"
  41. #include "rook_small_outline.bitmap"
  42. #include "knight_small_outline.bitmap"
  43. #include "bishop_small_outline.bitmap"
  44. #include "queen_small_outline.bitmap"
  45. #include "king_small_outline.bitmap"
  46.  
  47. static bool pos[32];
  48.  
  49. static piecetype pcs[] = { KING, QUEEN, ROOK, ROOK, BISHOP, BISHOP, KNIGHT,
  50.         KNIGHT, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN, PAWN } ;
  51.  
  52. static int piecepos();
  53. static char *bitsget();
  54.  
  55. void
  56. jail_init(win)
  57.     windata *win;
  58. {
  59.     int i;
  60.  
  61.     for (i = 0; i < 32; i++)
  62.         pos[i] = false;
  63.     jail_draw(win);
  64.     return;
  65. }
  66.  
  67. #define JAIL_HEADER    "Captured Pieces"
  68.  
  69. void
  70. jail_draw(win)
  71.     windata *win;
  72. {
  73.     int i;
  74.     char *bits;
  75.     Pixmap tmpPM;
  76.     piece p;
  77.  
  78.     i = XTextWidth(win->large, JAIL_HEADER, strlen(JAIL_HEADER));
  79.     XSetFont(win->display, DefaultGC(win->display, 0),
  80.          win->large->fid);
  81.     XSetForeground(win->display, DefaultGC(win->display, 0),
  82.                win->textcolor.pixel);
  83.     XSetBackground(win->display, DefaultGC(win->display, 0),
  84.                win->textback.pixel);
  85.     
  86.     XDrawImageString(win->display, win->jailwin,
  87.              DefaultGC(win->display, 0),
  88.              (JAIL_WIDTH - i) / 2, 20, JAIL_HEADER,
  89.             strlen(JAIL_HEADER));
  90.  
  91.     XSetForeground(win->display, DefaultGC(win->display, 0),
  92.                win->blackpiece.pixel);
  93.     XSetBackground(win->display, DefaultGC(win->display, 0),
  94.                win->textback.pixel);
  95.     XSetFillStyle(win->display, DefaultGC(win->display, 0),
  96.               FillSolid);
  97.     XSetFunction(win->display, DefaultGC(win->display, 0),
  98.              GXcopy);
  99.     
  100.     for (i = 0; i < 16; i++)
  101.         if (pos[i]) {
  102.             p.color = WHITE;
  103.             p.type = pcs[i];
  104.             bits = bitsget(&p);
  105.             tmpPM = XCreateBitmapFromData(win->display,
  106.                      win->jailwin, bits,
  107.                      32, 32);
  108.             
  109.             XCopyPlane(win->display, tmpPM, win->jailwin,
  110.                   DefaultGC(win->display, 0),
  111.                   0, 0, 32, 32,
  112.                   5 + (i % 8) * 32, 25 + (i / 8) * 32,
  113.                   1);
  114.                 XFreePixmap(win->display, tmpPM);
  115.         } else {
  116.             XFillRectangle(win->display, win->jailwin,
  117.                    DefaultGC(win->display, 0),
  118.                    5 + (i % 8) * 32,
  119.                    25 + (i / 8) * 32,
  120.                    32, 32);
  121.         }
  122.     for (i = 0; i < 16; i++)
  123.         if (pos[i + 16]) {
  124.             p.color = BLACK;
  125.             p.type = pcs[i];
  126.             bits = bitsget(&p);
  127.             tmpPM = XCreateBitmapFromData(win->display,
  128.                           win->jailwin, bits,
  129.                           32, 32);
  130.             
  131.             XCopyPlane(win->display, tmpPM, win->jailwin,
  132.                    DefaultGC(win->display, 0),
  133.                    0, 0, 32, 32,
  134.                    5 + (i % 8) * 32, 94 + (i / 8) * 32,
  135.                    1);
  136.             XFreePixmap(win->display, tmpPM);
  137.         } else {
  138.             XFillRectangle(win->display, win->jailwin,
  139.                    DefaultGC(win->display, 0),
  140.                    5 + (i % 8) * 32, 94 + (i / 8) * 32,
  141.                    32, 32);
  142.         }
  143.  
  144.     return;
  145. }
  146.  
  147. void
  148. jail_add(p)
  149.     piece *p;
  150. {
  151.     int i = piecepos(p, false);
  152.     char *bits;
  153.     Pixmap tmpPM;
  154.     
  155.     pos[i] = true;
  156.  
  157.     bits = bitsget(p);
  158.  
  159.     XSetState(win1->display, DefaultGC(win1->display, 0),
  160.                win1->blackpiece.pixel,
  161.                win1->textback.pixel,
  162.                GXcopy,
  163.                AllPlanes);
  164.  
  165.     tmpPM = XCreateBitmapFromData(win1->display,
  166.                       win1->jailwin, bits,
  167.                       32, 32);
  168.             
  169.     XCopyPlane(win1->display, tmpPM, win1->jailwin,
  170.            DefaultGC(win1->display, 0),
  171.            0, 0, 32, 32,
  172.            5 + (i % 8) * 32, ((i >= 16) ? 30 : 25) + (i / 8) * 32,
  173.            1);
  174.     XFreePixmap(win1->display, tmpPM);
  175.     
  176.     if (!oneboard) {
  177.         XSetState(win2->display, DefaultGC(win2->display, 0),
  178.               win2->blackpiece.pixel,
  179.               win2->textback.pixel,
  180.               GXcopy,
  181.               AllPlanes);
  182.  
  183.  
  184.         tmpPM = XCreateBitmapFromData(win2->display,
  185.                       win2->jailwin, bits,
  186.                       32, 32);
  187.             
  188.         XCopyPlane(win2->display, tmpPM, win2->jailwin,
  189.                DefaultGC(win2->display, 0),
  190.                0, 0, 32, 32,
  191.                5 + (i % 8) * 32, ((i >= 16) ? 30 : 25) + (i / 8) * 32,
  192.                1);
  193.         XFreePixmap(win2->display, tmpPM);
  194.     }
  195.  
  196.     return;
  197. }
  198.  
  199. void
  200. jail_remove(p)
  201.     piece *p;
  202. {
  203.     int i = piecepos(p, true);
  204.  
  205.     pos[i] = false;
  206.  
  207.  
  208.     XSetForeground(win1->display,
  209.            DefaultGC(win1->display, 0),
  210.            win1->blackpiece.pixel);
  211.     XSetBackground(win1->display,
  212.            DefaultGC(win1->display, 0),
  213.            win1->textback.pixel);
  214.     XSetFillStyle(win1->display,
  215.           DefaultGC(win1->display, 0),
  216.           FillSolid);
  217.  
  218.     XFillRectangle(win1->display, win1->jailwin,
  219.            DefaultGC(win1->display, 0),
  220.            5 + (i % 8) * 32,
  221.            ((i >= 16) ? 30 : 25) + (i / 8) * 32,
  222.            32, 32);
  223.  
  224.     if (!oneboard) {
  225.     XSetForeground(win2->display,
  226.                DefaultGC(win2->display, 0),
  227.                win2->blackpiece.pixel);
  228.     XSetBackground(win2->display,
  229.                DefaultGC(win2->display, 0),
  230.                win2->textback.pixel);
  231.     XSetFillStyle(win2->display,
  232.               DefaultGC(win2->display, 0),
  233.               FillSolid);
  234.  
  235.     XFillRectangle(win2->display, win2->jailwin,
  236.                DefaultGC(win2->display, 0),
  237.                5 + (i % 8) * 32,
  238.                ((i >= 16) ? 30 : 25) + (i / 8) * 32,
  239.                32, 32);
  240.     }
  241.  
  242.     return;
  243. }
  244.  
  245. static char *bitsget(p)
  246.     piece *p;
  247. {
  248.     char *bits;
  249.  
  250.     switch (p->type) {
  251.         case PAWN:
  252.         bits = (p->color == WHITE) ? pawn_small_outline_bits :
  253.                 pawn_small_bits;
  254.         break;
  255.  
  256.         case ROOK:
  257.         bits = (p->color == WHITE) ? rook_small_outline_bits :
  258.                 rook_small_bits;
  259.         break;
  260.  
  261.         case KNIGHT:
  262.         bits = (p->color == WHITE) ? knight_small_outline_bits :
  263.                 knight_small_bits;
  264.         break;
  265.  
  266.         case BISHOP:
  267.         bits = (p->color == WHITE) ? bishop_small_outline_bits :
  268.                 bishop_small_bits;
  269.         break;
  270.  
  271.         case QUEEN:
  272.         bits = (p->color == WHITE) ? queen_small_outline_bits :
  273.                 queen_small_bits;
  274.         break;
  275.  
  276.         case KING:
  277.         bits = (p->color == WHITE) ? king_small_outline_bits :
  278.                 king_small_bits;
  279.         break;
  280.     }
  281.     return (bits);
  282. }
  283.  
  284. static int
  285. piecepos(p, there)
  286.     piece *p;
  287.     bool there;
  288. {
  289.     int i, base = (p->color == WHITE) ? 0 : 16;
  290.  
  291.     switch (p->type) {
  292.         case PAWN:
  293.         for (i = base + 8; (i < base + 15) && pos[i]; i++)
  294.             ;
  295.         if (there && !pos[i])
  296.             i--;
  297.         break;
  298.  
  299.         case KING:
  300.         /* Hmm... */
  301.         i = base;
  302.         break;
  303.  
  304.         case QUEEN:
  305.         i = base + 1;
  306.         break;
  307.  
  308.         case ROOK:
  309.         i = base + 2;
  310.         if ((there && pos[i + 1]) || (!there && pos[i]))
  311.             i++;
  312.         break;
  313.  
  314.         case BISHOP:
  315.         i = base + 4;
  316.         if ((there && pos[i + 1]) || (!there && pos[i]))
  317.             i++;
  318.         break;
  319.  
  320.         case KNIGHT:
  321.         i = base + 6;
  322.         if ((there && pos[i + 1]) || (!there && pos[i]))
  323.             i++;
  324.         break;
  325.     }
  326.     return (i);
  327. }
  328.  
  329.